package org.pearl.thread.demo.test002;

import java.util.concurrent.TimeUnit;

/**
 * Created by TD on 2021/4/21
 */
public class SyncTest003 {
    private static final Object MUTEX = new Object();

    public static void main(String[] args) throws InterruptedException {

        Runnable runnable = () -> {
            synchronized (MUTEX) {
                System.out.println(Thread.currentThread().getName() + "获取到锁");
                try {
                    TimeUnit.SECONDS.sleep(10 );
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();

        TimeUnit.SECONDS.sleep(1);

        t2.start();

        System.out.println(t1.getState()); // TIMED_WAITING
        System.out.println(t2.getState()); // BLOCKED
        t1.interrupt();
        System.out.println(t2.getState()); // BLOCKED
    }
}
